<template>
    <LModalForm 
        :visible="visible"
        :confirmLoading="confirmLoading" 
        :title="title" 
        ref="ChangePwd" 
        :formAttributes="formAttributes" 
        @handleOk="handleOk" 
        @handleCancel="handleCancel"
        @handlerChange="handlerChange"></LModalForm>

</template>

<script>
import LModalForm from "@/components/lt/form/LModalForm"
import request from '@/utils/request'
import md5 from 'md5'

export default {
    name:'ChangePwd',
    components:{
        LModalForm
    },
    data(){
        return{
            title:'详情',
            visible:false,
            confirmLoading: false,
            model:{},
            formAttributes:{
              type:'',//collapse,tabs
              formItems:[
                {
                  title:'标签一',
                  columns:1,
                  fields:[
                    {field:'empPwd',label:'新密码',component:'password',rules: [{ required: true, message: '请输入新密码!' }]},
                    {field:'empPwd2',label:'确认密码',component:'password',rules: [{required: true, message: '请输入确认密码'}]},
                  ]
                },
              ]

            }


        }
    },
    methods:{
       
        edit (record) {
            this.visible = true;
            
        },
        handleOk(){
            const that = this;
            this.$refs.ChangePwd.getForm().validateFields((err, values) => {
                if (!err) {
                   
                    let formData = Object.assign(this.model, values);
                    if(formData.empPwd2!=formData.empPwd){
                        that.$message.error("两次输入密码不一致");
                        return false
                    }
                    formData.empPwd= md5(formData.empPwd);
                    formData.accountNo = this.$store.state.user.info.accountNo
                   
                    that.confirmLoading = true
                    request({url: '/sysEmp/changePwd',method: 'post',data: formData}).then((res)=>{
                        if(res.success){
                            that.$message.success(res.message);
                            that.close()
                            //退出系统
                            return this.$store.dispatch('Logout').then(() => {
                                that.$router.push({ path: '/user/login'})
                                location.reload()
                            })
                        }else{
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        
                    })
                    
                }
            })
        },
        handleCancel(){
            this.visible=false
        },
        close () {
            this.$emit('close');
            this.visible = false;
        },
        /**
         * 表单内容发生变化
         */
        handlerChange(changeItem){
            console.log(changeItem)
           
        }
    },
    created(){
        
    },
}
</script>